/**
 * PLM项目模拟数据服务
 * 提供接口测试和离线状态下的模拟数据
 */

// 创建模拟项目列表
const mockProjects = [
  {
    _id: 'mock-01',
    model: '设备控制系统',
    cate: '基础平台',
    status: 2,
    devManager: '张工',
    bizManager: '李经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 5,
    updateTime: Math.floor(Date.now() / 1000) - 3600,
    description: '基础设备管控平台项目',
    progress: 30,
    priority: 'high'
  },
  {
    _id: 'mock-02',
    model: '智能监控平台',
    cate: '物联网',
    status: 3,
    devManager: '王工',
    bizManager: '赵经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 10,
    updateTime: Math.floor(Date.now() / 1000) - 86400,
    description: '基于AI的智能设备监控平台',
    progress: 45,
    priority: 'normal'
  },
  {
    _id: 'mock-03',
    model: '数据分析工具',
    cate: '大数据',
    status: 4,
    devManager: '刘工',
    bizManager: '陈经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 15,
    updateTime: Math.floor(Date.now() / 1000) - 86400 * 2,
    description: '大数据分析处理平台',
    progress: 65,
    priority: 'high'
  },
  {
    _id: 'mock-04',
    model: '移动端应用',
    cate: '应用软件',
    status: 1,
    devManager: '孙工',
    bizManager: '钱经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 3,
    updateTime: Math.floor(Date.now() / 1000) - 3600 * 5,
    description: '企业移动应用平台',
    progress: 10,
    priority: 'normal'
  },
  {
    _id: 'mock-05',
    model: '网络安全系统',
    cate: '网络安全',
    status: 5,
    devManager: '周工',
    bizManager: '吴经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 20,
    updateTime: Math.floor(Date.now() / 1000) - 86400 * 3,
    description: '企业级网络安全防护系统',
    progress: 80,
    priority: 'high'
  },
  // 添加实际创建的两个项目作为模拟数据
  {
    _id: 'new-project-01',
    model: '霍尔传感器项目A',
    cate: '霍尔传感器',
    status: 1,
    devManager: '李工',
    bizManager: '张经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 1,
    updateTime: Math.floor(Date.now() / 1000) - 3600 * 2,
    description: '新建的霍尔传感器项目',
    progress: 5,
    priority: 'high',
    customerName: '客户A'
  },
  {
    _id: 'new-project-02',
    model: '磁编码器项目B',
    cate: '磁编码器',
    status: 1,
    devManager: '王工',
    bizManager: '李经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 2,
    updateTime: Math.floor(Date.now() / 1000) - 3600 * 4,
    description: '新建的磁编码器项目',
    progress: 10,
    priority: 'normal',
    customerName: '客户B'
  }
];

// 模拟项目详情数据
const mockProjectDetails = {
  'mock-01': {
    _id: 'mock-01',
    model: '设备控制系统',
    cate: '基础平台',
    status: 2,
    devManager: '张工',
    bizManager: '李经理',
    createTime: Math.floor(Date.now() / 1000) - 86400 * 5,
    updateTime: Math.floor(Date.now() / 1000) - 3600,
    description: '基础设备管控平台项目',
    progress: 30,
    priority: 'high',
    startTime: Math.floor(Date.now() / 1000) - 86400 * 10,
    endTime: Math.floor(Date.now() / 1000) + 86400 * 30,
    members: ['张工', '王工', '刘工'],
    milestones: [
      { name: '需求分析', completion: 100, date: Math.floor(Date.now() / 1000) - 86400 * 3 },
      { name: '设计阶段', completion: 60, date: Math.floor(Date.now() / 1000) + 86400 * 5 },
      { name: '开发阶段', completion: 0, date: Math.floor(Date.now() / 1000) + 86400 * 15 }
    ]
  }
};

// 获取项目列表 - 修改为返回Promise，与云函数格式一致
function getProjectList(params = {}) {
  console.log('[模拟服务] 获取项目列表', params);
  
  // 将处理逻辑包装在Promise中
  return new Promise((resolve) => {
    // 处理分页参数
    const page = params.page || 1;
    const size = params.size || 10;
    let filteredProjects = [...mockProjects];
    
    // 处理过滤条件
    if (params.search) {
      const keyword = params.search.toLowerCase();
      filteredProjects = filteredProjects.filter(p => 
        (p.model && p.model.toLowerCase().includes(keyword)) || 
        (p.description && p.description.toLowerCase().includes(keyword)) ||
        (p.customerName && p.customerName.toLowerCase().includes(keyword))
      );
    }
    
    if (params.status && params.status !== 'all') {
      filteredProjects = filteredProjects.filter(p => p.status == params.status);
    }
    
    if (params.cate && params.cate !== 0) {
      filteredProjects = filteredProjects.filter(p => p.cate === params.cate);
    }
    
    // 计算总数
    const total = filteredProjects.length;
    
    // 分页处理
    const start = (page - 1) * size;
    const end = start + size;
    const pagedProjects = filteredProjects.slice(start, end);
    
    console.log('[模拟服务] 过滤后项目数量:', filteredProjects.length, '分页后项目数量:', pagedProjects.length);
    
    // 详细记录返回的数据项目
    console.log('[模拟服务] 返回的项目数据:', JSON.stringify(pagedProjects.map(p => ({
      _id: p._id,
      model: p.model
    }))));
    
    // 直接返回与cloud_helper期望的格式完全一致的结构
    resolve({
      code: 0,
      msg: "ok",
      list: pagedProjects,
      total: total,
      page: page,
      size: size
    });
  });
}

// 获取项目详情 - 同样修改为返回Promise
function getProjectDetail(id) {
  console.log('[模拟服务] 获取项目详情', id);
  
  return new Promise((resolve) => {
    // 查找指定ID的详情数据
    let detail = mockProjectDetails[id];
    
    // 如果找不到详情数据，则从列表数据构建
    if (!detail) {
      const project = mockProjects.find(p => p._id === id);
      if (project) {
        detail = {
          ...project,
          startTime: Math.floor(Date.now() / 1000) - 86400 * 5,
          endTime: Math.floor(Date.now() / 1000) + 86400 * 30,
          members: ['开发者1', '开发者2'],
          milestones: [
            { name: '需求分析', completion: 100, date: Math.floor(Date.now() / 1000) - 86400 * 3 },
            { name: '设计阶段', completion: 30, date: Math.floor(Date.now() / 1000) + 86400 * 5 }
          ]
        };
      }
    }
    
    // 如果仍然没有找到，创建一个默认的详情数据
    if (!detail) {
      detail = {
        _id: id,
        model: '默认项目',
        cate: '其他',
        status: 1,
        devManager: '未指定',
        bizManager: '未指定',
        createTime: Math.floor(Date.now() / 1000) - 86400,
        updateTime: Math.floor(Date.now() / 1000),
        description: '这是一个默认创建的模拟项目',
        progress: 10,
        priority: 'normal',
        startTime: Math.floor(Date.now() / 1000) - 86400,
        endTime: Math.floor(Date.now() / 1000) + 86400 * 30,
        members: ['未指定'],
        milestones: [
          { name: '计划阶段', completion: 10, date: Math.floor(Date.now() / 1000) + 86400 * 5 }
        ]
      };
    }
    
    // 确保返回格式与cloud_helper期望的一致 - 直接返回detail而非包装在data中
    resolve({
      code: 0,
      msg: 'ok',
      data: detail
    });
  });
}

// 导出模块
module.exports = {
  getProjectList,
  getProjectDetail
}; 